﻿
Imports MySql.Data.MySqlClient

Public Class frmiQMySQLSetting
    Private Sub frmiQMySQLSetting_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Try
            LoadDefaultConfig()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try

        Me.MdiParent = frmMain

    End Sub

    Private iQMyClsConn As ClsSQLhelper

    Private Sub iQBtnCancel_Click_1(sender As System.Object, e As System.EventArgs) Handles iQBtnCancel.Click
        Me.Dispose()
    End Sub



    Private Sub iQBtnOK_Click_1(sender As System.Object, e As System.EventArgs) Handles iQBtnOK.Click
        Try



            If MsgBox("คุณต้องการแก้ไขค่า connection ใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.No Then Exit Sub
            If Me.TxtiQDatabase.Text.Trim.Equals("") Then
                MsgBox("กรุณาใส่ชื่อฐานข้อมูล", MsgBoxStyle.Exclamation)
                Exit Sub
            End If

            If Me.iQTxtiQUser.Text.Trim.Equals("") Then
                MsgBox("กรุณาใส่ชื่อผู้ใช้", MsgBoxStyle.Exclamation)
                Exit Sub
            End If

            If Me.TxtiQPassword.Text.Trim.Equals("") Then
                MsgBox("กรุณาใส่รหัสผ่าน", MsgBoxStyle.Exclamation)
            End If

            If Me.TxtiQPort.Text = "" Then
                MsgBox("กรุณาใส่พ็อต", MsgBoxStyle.Exclamation)
            ElseIf IsNumeric(Me.TxtiQPort.Text) = False Then
                MsgBox("กรุณาใส่เป็นตัวเลขเท่านั้น", MsgBoxStyle.Exclamation)
            End If

            If Me.iQTxtiQHost.Text.Trim.Equals("") Then
                MsgBox("กรุณาใส่ชื่อ Server", MsgBoxStyle.Exclamation)
            End If
            '(LoadDefaultConfig())
            If TestConnectionResult() = False Then
                MsgBox("ค่าที่ตั้งไม่สามารถติดต่อฐานข้อมูลได้ กรุณากรอกข้อมูลใหม่อีกครั้ง", MsgBoxStyle.Information, "ไม่สามารถปรับปรุงการตั้งค่าได้")
                Exit Sub
            Else
                'If (ClsSQLhelper.MySQLDatabase = Me.TxtDatabase.Text) And (ClsSQLhelper.MySQLUser = Me.TxtUser.Text) And (ClsSQLhelper.MySQLPassword = Me.TxtPassword.Text) And (ClsSQLhelper.MySQLPort = Me.TxtPort.Text) And (ClsSQLhelper.MySQLServer = Me.TxtHost.Text) Then
                '    MsgBox("บันทึกค่าสำเร็จ", MsgBoxStyle.Information)
                'Else

                'End If
                ClsSQLhelper.iQMySQLDatabase = Me.TxtiQDatabase.Text
                ClsSQLhelper.iQMySQLUser = Me.iQTxtiQUser.Text
                'ClsSQLhelper.MySQLPassword = Me.TxtPassword.Text
                ClsSQLhelper.iQMySQLPassword = Crypto.Encrypt(Me.TxtiQPassword.Text, "sys11266")
                ClsSQLhelper.iQMySQLPort = Me.TxtiQPort.Text
                ClsSQLhelper.iQMySQLServer = Me.iQTxtiQHost.Text
                MsgBox("บันทึกค่าสำเร็จ กรุณาปิดแล้วเปิดโปรแกรมใหม่เพื่อให้โปรแกรมใช้ค่าที่ตั้งใหม่", MsgBoxStyle.Information)
                End
            End If

            Me.Dispose()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Sub LoadDefaultConfig()
        Try
            Me.TxtiQDatabase.Text = ClsSQLhelper.iQMySQLDatabase
            Me.iQTxtiQUser.Text = ClsSQLhelper.iQMySQLUser
            Me.TxtiQPassword.Text = Crypto.Decrypt(ClsSQLhelper.iQMySQLPassword, "sys11266")
            'Me.TxtPassword.Text = ClsSQLhelper.MySQLPassword
            Me.TxtiQPort.Text = ClsSQLhelper.iQMySQLPort
            Me.iQTxtiQHost.Text = ClsSQLhelper.iQMySQLServer
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Private Sub iQBtnTest_Click_1(sender As System.Object, e As System.EventArgs) Handles iQBtnTest.Click
        TestConnectionResult()
    End Sub


    Private Sub TxtHost_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtiQHost.KeyPress
        Try
            If Asc(e.KeyChar) = 13 Then
                TestConnectionResult()
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Private Sub TxtUser_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles iQTxtiQUser.KeyPress
        Try
            If Asc(e.KeyChar) = 13 Then
                TestConnectionResult()
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Private Sub TxtPassword_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtiQPassword.KeyPress
        Try
            If Asc(e.KeyChar) = 13 Then
                TestConnectionResult()
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Private Sub TxtDatabase_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtiQDatabase.KeyPress
        Try
            If Asc(e.KeyChar) = 13 Then
                TestConnectionResult()
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Private Sub TxtPort_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TxtiQPort.KeyPress
        Try
            If Asc(e.KeyChar) = 13 Then
                TestConnectionResult()
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub
    Function TestConnectionResult() As Boolean

        Try
            ' MyClsConn.TestConnectionResult()
            TestConnectionResult = False
            Dim TestConnstr As String = "server=" & iQTxtiQHost.Text & ";user=" & iQTxtiQUser.Text & ";database=" & TxtiQDatabase.Text & ";port=" & TxtiQPort.Text & ";password=" & TxtiQPassword.Text & ";"
            Dim TestConn As New MySqlConnection
            Try
                TestConn.ConnectionString = TestConnstr
                TestConn.Open()

                MessageBox.Show("ติดต่อฐานข้อมูลสำเร็จ")
                TestConnectionResult = True
                TestConn.Close()
            Catch myerror As MySqlException
                TestConnectionResult = False
                MsgBox(Err.Number & " : " & myerror.Message, MsgBoxStyle.Critical, "ไม่สามารถติดต่อฐานข้อมูลได้")
            Finally
                TestConn.Dispose()
            End Try
        Catch ex As Exception
            MsgBox(Err.Number & " : " & ex.Message.ToString)
        End Try
    End Function


    
   
End Class
